#include "Defect_Rect.h"
#include "qdebug.h"
Defect_Rect::Defect_Rect()
{

}
int Defect_Rect::D_Rect(HObject Img, HObject &OutImg)
{
	HTuple  parm;
	//参数 1  产品定位阈值
	parm[0] = m_part.para1;
	//**参数2 外部缺陷阈值
	parm[1] = m_part.para2;
	//**参数3 外部缺陷面积
	parm[2] = m_part.para3;
	//**参数4 外部内缩宽度
	parm[3] = m_part.para4;
	// **参数5 外部内缩高度
	parm[4] = m_part.para5;
	//**参数6  内部定位阈值
	parm[5] = m_part.para6;
	//**参数7 内部缺陷面积
	parm[6] = m_part.para7;
	//**参数8 大崩边面积
	parm[7] = m_part.para8;
	//**参数9  是否开启划痕检测
	parm[8] = m_part.para9;
	//**参数10  内部白划痕检测阈值
	parm[9] = m_part.para10;
	//**参数11  内部黑划痕检测阈值
	parm[10] = m_part.para11;
	//parm[10] = 3;
	//**参数12  内部缺陷检测阈值
	parm[11] = m_part.para12;
	//**参数13 内部内缩宽度
	parm[12] = m_part.para13;
	//**参数14  内部内缩高度
	parm[13] = m_part.para14;
	//**参数15  是否带导角
	parm[14] = m_part.para15;
	//内部线痕面积
	parm[15] = m_part.para16;
	//是否开启斑纹检测
	parm[16] = m_part.para17;
	//斑纹检测阈值
	parm[17] = m_part.para18;
	//斑纹检测面积
	parm[18] = m_part.para19;
	//斑纹检测长度
	parm[19] = m_part.para20;

	////是否开启斑纹检测
	//parm[16] = 1;
	////斑纹检测阈值
	//parm[17] = 25;
	////斑纹检测面积
	//parm[18] = 40;
	////斑纹检测长度
	//parm[19] = 40;



	int flag = 0;


	HObject  Rect, ImageReduced;
	HObject  Region, ConnectedRegions, SelectedRegions;
	HObject  ImageRotate, ImageReduced1, Region1, ConnectedRegions1;
	HObject  SelectedRegions1, RegionFillUp, ImageReduced2;
	HObject  Region2, ConnectedRegions2, SelectedRegions2;
	HObject  ImageReduced3;
	HObject  Region3, SelectedRegions3;
	HObject  RegionTrans1, RegionDilation, RegionDifference;
	HObject  ImageResult1, ImageResult2, ImageResult3;
	HObject  MultiChannelImage, RegionTrans2, RegionDifference1, RegionErosion1, RegionUnion;

	HObject RegionTrans22, RegionFillUp22, RegionDifference22, RegionErosion22;


	HTuple  Width, Height, Row, Column;
	HTuple  Value, Length1, Length2, Phi, Number1, Number2, Number;
	HTuple  HomMat2DIdentity, HomMat2DRotate;

	HTuple area1, area2, Mean, Deviation;
	HTuple row1, col1, row2, col2;



	HObject RegionFillUp2, Region22, ImageReduced22, SelectedRegions22, RegionMirror, ConnectedRegions22, Region4, RegionAffineTrans, SelectedRegions5;
	HTuple H, W, Area1, Row1, Column1, Area2, Row2, Column2, HomMat2DTranslate;



	Number2 = 0;
	Number1 = 0;
	Number = 0;
	try
	{
		OutImg = Img;
		GetImageSize(Img, &Width, &Height);
		GenEmptyObj(&MultiChannelImage);


		GenRectangle1(&Rect, m_part2.x1, m_part2.y1, m_part2.x2, m_part2.y2);
		ReduceDomain(Img, Rect, &ImageReduced);

		Threshold(ImageReduced, &Region, 0, parm[0]);
		Connection(Region, &ConnectedRegions);
		SelectShapeStd(ConnectedRegions, &SelectedRegions, "max_area", 70);

		SmallestRectangle2(SelectedRegions, &Row, &Column, &Value, &Length1,
			&Length2);
		if (Row.D() == 0)
		{
			flag = 2;
			AreaValue.Append(0).Append(0);
			return flag;
		}

		if (0 != (Value > (PI / 4)))
		{
			Phi = Value - (PI / 2);
			RotateImage(Img, &ImageRotate, (((PI / 2) - Value) * 180) / PI, "constant");

		}
		else if (0 != (HTuple(Value<((-PI) / 4)).TupleAnd(Value>((-PI) / 2))))
		{
			Phi = Value + (PI / 2);
			RotateImage(Img, &ImageRotate, ((-(Value + (PI / 2))) * 180) / PI, "constant");
		}
		else if (0 != (HTuple(Value<PI / 4).TupleAnd(Value>((-PI) / 4))))
		{
			Phi = Value;
			RotateImage(Img, &ImageRotate, ((-Value) * 180) / PI, "constant");
		}
		else
		{
			Phi = -Value;
			RotateImage(Img, &ImageRotate, (Value * 180) / PI, "constant");

		}

		ReduceDomain(ImageRotate, Rect, &ImageReduced1);
		Threshold(ImageReduced1, &Region1, 0, parm[1]);
		FillUp(Region1, &Region1);
		Connection(Region1, &ConnectedRegions1);
		SelectShapeStd(ConnectedRegions1, &SelectedRegions1, "max_area", 70);
		FillUp(SelectedRegions1, &RegionFillUp);

		//**大型崩边

		ShapeTrans(RegionFillUp, &RegionTrans22, "rectangle2");
		Difference(RegionTrans22, RegionFillUp, &RegionDifference22);
		ErosionRectangle1(RegionDifference22, &RegionErosion22, parm[3], parm[4]);
		Connection(RegionErosion22, &RegionErosion22);
		SelectShape(RegionErosion22, &RegionErosion22, "area", "and", parm[7], 999999);
		Union1(RegionErosion22, &RegionErosion22);

		if (parm[14] == 0)
		{
			ShapeTrans(RegionFillUp, &RegionTrans2, "rectangle2");
		}
		else
		{
			ShapeTrans(RegionFillUp, &RegionTrans2, "convex");
		}

		Difference(RegionTrans2, RegionFillUp, &RegionDifference1);
		ErosionRectangle1(RegionDifference1, &RegionErosion1, parm[3], parm[4]);
		Connection(RegionErosion1, &RegionErosion1);
		SelectShape(RegionErosion1, &RegionErosion1, "area", "and", parm[2], 99999);
		Union1(RegionErosion1, &RegionErosion1);
		Union2(RegionErosion1, RegionErosion22, &RegionErosion1);
		if (Outer)
		{
			CountObj(RegionErosion1, &Number1);

		}
		else
		{
			Number1 = 0;
		}

		Threshold(ImageRotate, &Region2, 0, 100);
		ReduceDomain(Region2, Rect, &Region2);
		FillUp(Region2, &RegionFillUp2);
		Connection(RegionFillUp2, &ConnectedRegions2);
		SelectShapeStd(ConnectedRegions2, &SelectedRegions2, "max_area", 70);
		ErosionRectangle1(SelectedRegions2, &SelectedRegions2, 5, 5);
		ReduceDomain(ImageRotate, SelectedRegions2, &ImageReduced2);
		Threshold(ImageReduced2, &Region22, 60, 255);


		if (parm[14] == 0)
		{
			ShapeTrans(Region22, &RegionTrans2, "rectangle2");
		}
		else
		{
			ShapeTrans(Region22, &RegionTrans2, "convex");
		}

		ReduceDomain(ImageReduced2, RegionTrans2, &ImageReduced22);
		Threshold(ImageReduced22, &Region3, 0, parm[5]);
		FillUp(Region3, &RegionFillUp22);
		ErosionRectangle1(RegionFillUp22, &RegionFillUp22, 2, 2);
		Connection(RegionFillUp22, &RegionFillUp22);
		SelectShapeStd(RegionFillUp22, &SelectedRegions22, "max_area", 70);
		DilationRectangle1(SelectedRegions22, &SelectedRegions22, 2, 2);



		if (parm[14] == 0)
		{
			ShapeTrans(SelectedRegions22, &RegionTrans22, "rectangle2");
		}
		else
		{
			ShapeTrans(SelectedRegions22, &RegionTrans22, "convex");
		}




		ErosionRectangle1(RegionTrans22, &RegionTrans22, parm[12], parm[13]);
		RegionFeatures(RegionTrans22, "height", &H);
		RegionFeatures(RegionTrans22, "width", &W);
		if (0 != (W>H))
		{
			MirrorRegion(RegionTrans22, &RegionMirror, "row", Height);
		}
		else
		{
			MirrorRegion(RegionTrans22, &RegionMirror, "column", Height);

		}
		AreaCenter(RegionTrans22, &Area1, &Row1, &Column1);
		AreaCenter(RegionMirror, &Area2, &Row2, &Column2);


		HomMat2dIdentity(&HomMat2DIdentity);
		HomMat2dTranslate(HomMat2DIdentity, Row1 - Row2, Column1 - Column2,
			&HomMat2DTranslate);
		AffineTransRegion(RegionMirror, &RegionAffineTrans, HomMat2DTranslate,
			"nearest_neighbor");
		Union2(RegionAffineTrans, RegionTrans22, &RegionTrans22);

		ReduceDomain(ImageRotate, RegionTrans22, &ImageReduced3);

		//内部通用缺陷检测
		Intensity(RegionTrans22, ImageReduced3, &Mean, &Deviation);
		Threshold(ImageReduced3, &Region4, (Mean + Deviation) + parm[11], 255);
		Connection(Region4, &ConnectedRegions22);
		SelectShape(ConnectedRegions22, &SelectedRegions3, "area", "and", parm[6], 99999);
		HTuple aa;
		CountObj(SelectedRegions3,&aa);
		int aaa = aa.D();

		//检测斑纹
		HObject ho_ImagePart, ho_Rectangle1, ho_Rectangle2, ho_ImageFFT1, ho_ImageSub, ho_SelectedRegions, ho_ConnectedRegions, ho_Region, ho_ImageResult, ho_RegionUnion;
		HObject ho_ImageFFT, ho_ImageReduced3, ho_RegionDilation, ho_RegionDifference, ho_ImageResult2, ho_ImageResult1, ho_ImageResult3;
		HTuple hv_Width, hv_Height, ho_Num;
		if (parm[16] == 1)
		{
			Threshold(ImageReduced3, &ho_ImageReduced3, 0, 128);
			ShapeTrans(ho_ImageReduced3, &ho_ImageReduced3, "outer_circle");
			DilationCircle(ho_ImageReduced3, &ho_RegionDilation, 4);
			Difference(ho_RegionDilation, ho_ImageReduced3, &ho_RegionDifference);

			CropDomain(ImageReduced3, &ho_ImagePart);
			GetImageSize(ho_ImagePart, &hv_Width, &hv_Height);
			GenRectangle1(&ho_Rectangle1, 0, (hv_Width / 2) - 2, (hv_Height / 2) - 10, (hv_Width / 2) + 2);
			GenRectangle1(&ho_Rectangle2, (hv_Height / 2) + 10, (hv_Width / 2) - 2, hv_Height, (hv_Width / 2) + 2);
			Union2(ho_Rectangle1, ho_Rectangle2, &ho_RegionUnion);
			FftGeneric(ho_ImagePart, &ho_ImageFFT, "to_freq", -1, "sqrt", "dc_center", "complex");
			PaintRegion(ho_RegionUnion, ho_ImageFFT, &ho_ImageResult, 0, "fill");
			FftGeneric(ho_ImageResult, &ho_ImageFFT1, "from_freq", 1, "sqrt", "dc_center",
				"byte");
			SubImage(ho_ImageFFT1, ho_ImagePart, &ho_ImageSub, 1, 10);
			//parm[17]=25;
			Threshold(ho_ImageSub, &ho_Region, parm[17], 255);
			Connection(ho_Region, &ho_ConnectedRegions);
			SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "row", "and", 5, hv_Height - 10);
			// parm[18]=40 parm[19]=50
			SelectShape(ho_SelectedRegions, &ho_SelectedRegions, (HTuple("area").Append("width")),
				"and", (HTuple(parm[18]).Append(parm[19])), (HTuple(99999).Append(99999)));
			CountObj(ho_SelectedRegions, &ho_Num);
			if (ho_Num.D()>0)
			{
				PaintRegion(ho_RegionDifference, Img, &ho_ImageResult1, 255, "fill");
				PaintRegion(ho_RegionDifference, Img, &ho_ImageResult2, 0, "fill");
				PaintRegion(ho_RegionDifference, Img, &ho_ImageResult3, 0, "fill");
				Compose3(ho_ImageResult1, ho_ImageResult2, ho_ImageResult3, &MultiChannelImage);
				flag = 1;
				OutImg = MultiChannelImage;
				AreaValue.Append(0).Append(0);
				return 1;
			}
		}


		//划痕检测
		HObject RegionTrans33, Rectangle33, Region33_2, ImageReduced33, SelectedRegions33,SelectedRegions44_2;
		HObject Rectangle44, ImageReduced44, SelectedRegions44;
		HObject RegionIntersection, SelectedRegions55, Region33_1, Region44_1, EmptyObject33_1, EmptyObject44_1;
		HObject EmptyObject33_2, EmptyObject44_2;
		HObject Region44_2, SelectedRegions44_1, SelectedRegions44_11, SelectedRegions44_12, SelectedRegions44_111, Empty44_1;
		HObject Empty44_2, EmptyObject44_21, SelectedRegions44_22, SelectedRegions44_222, SelectedRegions44_21;
		HObject Empty33_1, SelectedRegions33_1, SelectedRegions33_12, SelectedRegions33_11, SelectedRegions33_111;
		HObject Empty33_2, SelectedRegions33_2, SelectedRegions33_22, SelectedRegions33_21, SelectedRegions33_222;
		HObject RegionIntersection1, RegionIntersection2, SelectedRegions_Line;
		HTuple W1, H1, Area3, Row3, Column3, Mean33, Deviation33, Mean44, Deviation44;
		if (parm[8] == 1)
		{

			GenEmptyObj(&EmptyObject33_1);
			GenEmptyObj(&EmptyObject44_1);
			GenEmptyObj(&EmptyObject33_2);
			GenEmptyObj(&EmptyObject44_2);
			ShapeTrans(RegionTrans22, &RegionTrans33, "inner_rectangle1");
			RegionFeatures(RegionTrans33, "width", &W1);
			RegionFeatures(RegionTrans33, "height", &H1);
			AreaCenter(RegionTrans33, &Area3, &Row3, &Column3);


			//垂直方向
			int Step1 = W1.D() / 40;
			for (int i = 1; i <= 40; i++)
			{
				GenRectangle2(&Rectangle33, Row3, Column3 - W1 / 2 + (Step1 / 2) + Step1*(i), 0, Step1 / 2, H1 / 2);
				ReduceDomain(ImageReduced3, Rectangle33, &ImageReduced33);
				Intensity(Rectangle33, ImageReduced33, &Mean33, &Deviation33);
				Threshold(ImageReduced33, &Region33_1, Mean33 + parm[9] + Deviation33, 255);
				if ((Mean33 - parm[10] - Deviation33) < 0)
				{
					Threshold(ImageReduced33, &Region33_2, 0, 1);
				}
				else
				{
					Threshold(ImageReduced33, &Region33_2, 0, Mean33 - parm[10] - Deviation33);
				}

				
				Union2(EmptyObject33_1, Region33_1, &EmptyObject33_1);
				Union2(EmptyObject33_2, Region33_2, &EmptyObject33_2);
			}
			//白线条
			CopyObj(EmptyObject33_1, &Empty33_1, 1, 1);
			Connection(EmptyObject33_1, &EmptyObject33_1);
			SelectShape(EmptyObject33_1, &SelectedRegions33_1, (HTuple("area").Append("circularity")),
				"and", HTuple(parm[15]).TupleConcat(0), (HTuple(99999).Append(0.2)));

			DilationCircle(SelectedRegions33_1, &SelectedRegions33_1, 4);
			Union1(SelectedRegions33_1, &SelectedRegions33_1);
			Connection(SelectedRegions33_1, &SelectedRegions33_1);
			SelectShape(SelectedRegions33_1, &SelectedRegions33_11, "area", "and", 350, 99999);

			SelectShape(EmptyObject33_1, &SelectedRegions33_12, (HTuple("area").Append("circularity")),
				"and", HTuple(100).TupleConcat(0.2), (HTuple(99999).Append(1)));

			Union2(SelectedRegions33_11, SelectedRegions33_12, &SelectedRegions33_111);


			//黑线条
			CopyObj(EmptyObject33_2, &Empty33_2, 1, 1);
			Connection(EmptyObject33_2, &EmptyObject33_2);
			SelectShape(EmptyObject33_2, &SelectedRegions33_2, (HTuple("area").Append("circularity")),
				"and", HTuple(parm[15]).TupleConcat(0), (HTuple(99999).Append(0.2)));

			DilationCircle(SelectedRegions33_2, &SelectedRegions33_2, 4);
			Union1(SelectedRegions33_2, &SelectedRegions33_2);
			Connection(SelectedRegions33_2, &SelectedRegions33_2);
			SelectShape(SelectedRegions33_2, &SelectedRegions33_21, "area", "and", 350, 99999);

			SelectShape(EmptyObject33_2, &SelectedRegions33_22, (HTuple("area").Append("circularity")),
				"and", HTuple(100).TupleConcat(0.2), (HTuple(99999).Append(1)));

			Union2(SelectedRegions33_21, SelectedRegions33_22, &SelectedRegions33_222);

			Union2(SelectedRegions33_111, SelectedRegions33_222, &SelectedRegions33);


			//水平方向
			int Step2 = H1.D() / 40;
			for (int i = 1; i <= 40; i++)
			{
				GenRectangle2(&Rectangle44, Row3 - H1 / 2 + (Step2 / 2) + Step2*(i), Column3, 0, W1 / 2, Step2 / 2);
				ReduceDomain(ImageReduced3, Rectangle44, &ImageReduced44);
				Intensity(Rectangle44, ImageReduced44, &Mean44, &Deviation44);
				Threshold(ImageReduced44, &Region44_1, Mean44 + parm[9] + Deviation44, 255);
				if ((Mean44 - parm[10] - Deviation44) < 0)
				{
					Threshold(ImageReduced44, &Region44_2, 0, 1);
				}
				else
				{
					Threshold(ImageReduced44, &Region44_2, 0, Mean44 - parm[10] - Deviation44);
				}
				
				//para[9]=25;
				Union2(EmptyObject44_1, Region44_1, &EmptyObject44_1);
				Union2(EmptyObject44_2, Region44_2, &EmptyObject44_2);
			}
			//白线条
			CopyObj(EmptyObject44_1, &Empty44_1, 1, 1);
			Connection(EmptyObject44_1, &EmptyObject44_1);
			SelectShape(EmptyObject44_1, &SelectedRegions44_1, (HTuple("area").Append("circularity")),
				"and", HTuple(parm[15]).TupleConcat(0), (HTuple(99999).Append(0.2)));
			DilationCircle(SelectedRegions44_1, &SelectedRegions44_1, 4);
			Union1(SelectedRegions44_1, &SelectedRegions44_1);
			Connection(SelectedRegions44_1, &SelectedRegions44_1);
			SelectShape(SelectedRegions44_1, &SelectedRegions44_11, "area", "and", 350, 99999);
			SelectShape(EmptyObject44_1, &SelectedRegions44_12, (HTuple("area").Append("circularity")),
				"and", HTuple(100).TupleConcat(0.3), (HTuple(99999).Append(1)));
			Union2(SelectedRegions44_11, SelectedRegions44_12, &SelectedRegions44_111);
			//黑线条
			CopyObj(EmptyObject44_2, &Empty44_2, 1, 1);
			Connection(EmptyObject44_2, &EmptyObject44_2);
			SelectShape(EmptyObject44_2, &SelectedRegions44_2, (HTuple("area").Append("circularity")),
				"and", HTuple(parm[15]).TupleConcat(0), (HTuple(99999).Append(0.2)));
			DilationCircle(SelectedRegions44_2, &SelectedRegions44_2, 4);
			Union1(SelectedRegions44_2, &SelectedRegions44_2);
			Connection(SelectedRegions44_2, &SelectedRegions44_2);
			SelectShape(SelectedRegions44_2, &SelectedRegions44_21, "area", "and", 350, 99999);
			SelectShape(EmptyObject44_2, &SelectedRegions44_22, (HTuple("area").Append("circularity")),
				"and", HTuple(100).TupleConcat(0.3), (HTuple(99999).Append(1)));
			Union2(SelectedRegions44_21, SelectedRegions44_22, &SelectedRegions44_222);

			Union2(SelectedRegions44_111, SelectedRegions44_222, &SelectedRegions44);




			//交集------------------
			//白线
			Intersection(Empty33_1, Empty44_1, &RegionIntersection1);
			Connection(RegionIntersection1, &RegionIntersection1);
			SelectShape(RegionIntersection1, &RegionIntersection1, "area", "and", HTuple(parm[15]), HTuple(9999999));
			DilationCircle(RegionIntersection1, &RegionIntersection1, 4);
			Union1(RegionIntersection1, &RegionIntersection1);
			Connection(RegionIntersection1, &RegionIntersection1);
			SelectShape(RegionIntersection1, &RegionIntersection1, "area", "and", 350, 99999);
			Union1(RegionIntersection1, &RegionIntersection1);
			//黑线
			Intersection(Empty33_2, Empty44_2, &RegionIntersection2);
			Connection(RegionIntersection2, &RegionIntersection2);
			SelectShape(RegionIntersection2, &RegionIntersection2, "area", "and", HTuple(parm[15]), HTuple(9999999));
			DilationCircle(RegionIntersection2, &RegionIntersection2, 4);
			Union1(RegionIntersection2, &RegionIntersection2);
			Connection(RegionIntersection2, &RegionIntersection2);
			SelectShape(RegionIntersection2, &RegionIntersection2, "area", "and", 350, 99999);
			Union1(RegionIntersection2, &RegionIntersection2);




			Union2(SelectedRegions33, SelectedRegions44, &SelectedRegions55);
			Union2(RegionIntersection1, RegionIntersection2, &RegionIntersection);


			Union2(RegionIntersection, SelectedRegions55, &SelectedRegions_Line);
			Union2(SelectedRegions3, SelectedRegions_Line, &SelectedRegions3);
			Connection(SelectedRegions3, &SelectedRegions3);
		}


		if (Inner)
		{
			CountObj(SelectedRegions3, &Number2);
		}
		else
		{
			Number2 = 0;
		}

		if (Number1 > 0 && Number2 > 0)
		{
			ShapeTrans(RegionErosion1, &RegionTrans1, "outer_circle");
			AreaCenter(RegionErosion1, &area1, &col1, &row1);

			ShapeTrans(SelectedRegions3, &RegionTrans2, "outer_circle");
			AreaCenter(SelectedRegions3, &area2, &col2, &row2);
			Union2(RegionTrans1, RegionTrans2, &RegionUnion);
			CountObj(RegionUnion, &Number);
			TupleConcat(area1[0], area2[0], &AreaValue);
		}
		else if (Number1 == 0 && Number2 > 0)
		{
			ShapeTrans(SelectedRegions3, &RegionTrans2, "outer_circle");
			AreaCenter(SelectedRegions3, &area2, &col2, &row2);
			Union1(RegionTrans2, &RegionUnion);
			CountObj(RegionUnion, &Number);

			TupleConcat(0, area2[0], &AreaValue);
		}
		else if (Number1 > 0 && Number2 == 0)
		{
			ShapeTrans(RegionErosion1, &RegionTrans1, "outer_circle");
			AreaCenter(RegionErosion1, &area1, &col1, &row1);
			Union1(RegionTrans1, &RegionUnion);
			CountObj(RegionUnion, &Number);
			TupleConcat(area1[0], 0, &AreaValue);
		}
		else if ((Number1 == 0) && (Number2 == 0))
		{
			Number = 0;
			TupleConcat(0, 0, &AreaValue);
		}

		if (0 != (Number > 0))
		{

			DilationCircle(RegionUnion, &RegionDilation, 4);

			Difference(RegionDilation, RegionUnion, &RegionDifference);
			HomMat2dIdentity(&HomMat2DIdentity);
			HomMat2dRotate(HomMat2DIdentity, Phi, Height / 2, Width / 2, &HomMat2DRotate);
			AffineTransRegion(RegionDifference, &RegionDifference, HomMat2DRotate,
				"nearest_neighbor");

			PaintRegion(RegionDifference, Img, &ImageResult1, 255, "fill");
			PaintRegion(RegionDifference, Img, &ImageResult2, 0, "fill");
			PaintRegion(RegionDifference, Img, &ImageResult3, 0, "fill");
			Compose3(ImageResult1, ImageResult2, ImageResult3, &MultiChannelImage);
			flag = 1;
			OutImg = MultiChannelImage;
		}

		return flag;

	}
	catch (HException)
	{
		flag = 2;
		AreaValue.Append(0).Append(0);
		return flag;
	}

}
